package com.xjl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.xjl.dao.UserDao;
import com.xjl.demo.User;
import com.xjl.util.DBUtil;

public class UserDaoImpl implements UserDao{

	//查询所有会员的信息
	public List<User> SelectAllUser(int pageStart, int pageSize) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<User> list = new ArrayList<User>();
		try {
			conn = DBUtil.getConnection();
			String sql = "SELECT * FROM t_user limit ?,?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, pageStart);
			stmt.setInt(2, pageSize);
			rs = stmt.executeQuery();
			while(rs.next()){
				User user =new User();
				user.setUid(rs.getInt("uid"));
				user.setLoginname(rs.getString("loginname"));
				user.setLoginpass(rs.getString("Loginpass"));
				user.setEmail(rs.getString("email"));
				user.setPhone(rs.getString("phone"));
				list.add(user);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, stmt, conn);
		}
		return list;
	}

	//查询总共有多少个会员
	public int CountUserDao() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs=null;
		int index = 0;
		try {
			conn = DBUtil.getConnection();
			String sql = "SELECT count(*) FROM t_user";
			stmt = conn.prepareStatement(sql);
			rs=stmt.executeQuery();
			if(rs.next()){
				index=rs.getInt(1);
			}
		}catch (Exception e) {
			index=0;
			//e.printStackTrace();	
		}finally{
			DBUtil.close(rs, stmt, conn);
		}
		return index;
	}

	@Override
	public int DeleteUserByIdDao(int uid) {
		Connection conn = null;
		PreparedStatement stmt = null;
		int index=0;
		try {
			conn = DBUtil.getConnection();
			String sql = "DELETE from t_user where uid=? ";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, uid);
			index=stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(stmt, conn);
		}
		return index;
	}
}